package com.d3works.apps.account.service.invoice.workflow.validate;

import com.d3works.apps.account.db.Invoice;
import com.d3works.apps.message.exception.D3worksMessageException;
import com.d3works.apps.message.service.TemplateMessageService;
import com.d3works.exception.D3worksException;
import com.d3works.exception.db.repo.TraceBackRepository;
import com.d3works.exception.service.TraceBackService;
import com.d3works.inject.Beans;

public class WorkflowValidationServiceImpl implements WorkflowValidationService {

  @Override
  public void afterValidation(Invoice invoice) throws D3worksException {
    // send message
    if (invoice.getInvoiceAutomaticMailOnValidate()) {
      try {
        Beans.get(TemplateMessageService.class)
            .generateAndSendMessage(invoice, invoice.getInvoiceMessageTemplateOnValidate());
      } catch (Exception e) {
        TraceBackService.trace(
            new D3worksMessageException(
                e, invoice, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR));
      }
    }
  }
}
